import os
import queue
import sys
import threading
import requests
import redis
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 设置标准输出为无缓冲
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', buffering=1)  # 行缓冲
DEBGUG = False 
DEBGUG = True # 本地环境
#是否把数据推送到数据库
IS_PUSH_DB=True
#IS_PUSH_DB=False # 本地环境
if DEBGUG:
    Q = queue.Queue()
    R = redis.Redis(host='127.0.0.1', port=6379, db=6)
else:
    Q = queue.Queue()
    R = redis.Redis(host='localhost', port=6379, db=3)
    R = redis.Redis(host='localhost', port=6379, db=6)
# 测试redis是否连接成功
try:
    R.ping()
    print(f"Redis 连接成功!")
except redis.ConnectionError:
    print(f"Redis 连接失败，请检查服务器是否启动")

#远程推送地址
if DEBGUG:
    REMOTE_DB_PUSH_URL =[
        "https://xf-162-test.lottefuture.com/api/douPush",
        "https://xf-162.lottefuture.com/api/douPush",

    ]
else:
    REMOTE_DB_PUSH_URL =[
        "https://xf-162-test.lottefuture.com/api/douPush",
        "https://xf-162.lottefuture.com/api/douPush",
    ]
#解析产品的接口
PARSE_PRODUCT="http://cgi-bin.lottefuture.com/python/link-parse.php"

#没有解析出来的产品url
NO_PARSE_DEFAULT_URL="https://uland.taobao.com/coupon/edetail?itemId=prbDpZjUxt6z52MseAOIptm-9za58Xoh72J0DyWWHrR"

# 创建一个互斥锁  互斥发送回答问题的请求
LOCK = threading.Lock()

# 采集间隔时间 分钟  0 表示不限制收到信息就采集
MINUTE_GAP = 0

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
    "Host": "www.douban.com",
    "Connection":"Close",
}
# 过滤不是本平台的URL
NO_URL=['douban'];
def is_link2(link_url):
    flag=False

    for v in NO_URL:

        if v in link_url:
            flag=True
            return flag
    return flag



# 隧道域名:端口号
#tunnel = "u705.kdltps.com:15818"
tunnel = "tun-festhu.qg.net:17690"

# 用户名密码方式
#username = "t14920023015118"
#password = "lny2ogcg"
username = "E5F6D6EE"
password = "9B219197EE72"

#print(proxy_url)
PROXIES = {
    "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
    "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
}




# 测试代理
# try:
#     response = requests.get("http://httpbin.org/ip", proxies=PROXIES, timeout=10)
#     print("代理生效，当前 IP:", response.json())  #代理服务器的 IP
# except requests.exceptions.ProxyError:
#     print("代理认证失败，请检查用户名/密码")
# except requests.exceptions.ConnectTimeout:
#     print("代理连接超时，可能已失效")



if  DEBGUG:
    # PROXIES = {
    #     "http": "http://127.0.0.1:9000",
    #     "https": "http://127.0.0.1:9000"
    # }
    COOKIE_PATH = r"./douban_cookies.json"
    ANWSER_PATH = r"./answer.txt"
else:
    COOKIE_PATH = r"./douban_cookies.json"
    ANWSER_PATH = r"./answer.txt"

# 平台
PLATFORM_LIS = ["京东", "淘宝","拼多多","唯品会"]

# 平台解析接口
URL_MAP = {
        "京东": "http://cgi-bin.lottefuture.com/python/jd-goods-query.php",
        "淘宝": "http://cgi-bin.lottefuture.com/python/taobao-goods-query.php",
        "拼多多":"http://cgi-bin.lottefuture.com/python/pdd-goods-query.php",
        "唯品会":"http://cgi-bin.lottefuture.com/python/vipshop-goods-query.php",
    }


